package com.elock.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.elock.base.core.controller.BaseController;
import com.elock.base.core.controller.ResultMsg;
import com.elock.base.core.dao.impl.NativeDao;
import com.elock.service.LoginOutService;
import com.elock.service.MemberService;

/**
 * 登陆控制器
 * @author 仁德
 */
@Controller("loginController")
@RequestMapping("/login")
public class LoginController extends BaseController {

	@Resource(name = "nativeDao")
	private NativeDao nativeDao;
	@Resource(name = "memberServiceImpl")
	private MemberService memberService;
	@Resource(name = "loginOutServiceImpl")
	private LoginOutService loginOutService;

	/**
	 * login
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String index() {

		return "login";
	}

	/**
	 * submit
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping(value = "/submit", method = RequestMethod.POST)
	public @ResponseBody ResultMsg submit(String username, String password,
			String companyName, HttpServletRequest request,
			HttpServletResponse response) {
//		if (username == null) {
//			ExceptionUtil.throwServiceException("请填写用户名");
//		}
//		if (password == null) {
//			ExceptionUtil.throwServiceException("请填写密码");
//		}
//		String sql = "select id,is_enabled from member where username = ? and password = ?";
//		Map<String, Object> maps = nativeDao.findSingleMap(sql,
//				new Object[] { username, DigestUtils.md5Hex(password) });
//		if (maps != null) {
//
//			Boolean isEnabled = (Boolean) maps.get("is_enabled");
//			if (!isEnabled) {
//				ExceptionUtil.throwServiceException("该账号已被禁用");
//			}
//
//			Long id = Long.parseLong(maps.get("id").toString());
//			WebUtils.setPrincipal(id, username, null, null, "ChinaCN", 0);
//			return success().addObjX("/index.jhtml");
//		}
//		else {
//			ExceptionUtil.throwServiceException("用户名或密码错误");
//		}
//		return error(null);
		loginOutService.submitLogin(username, password, companyName, request, response);
		return success().addObjX("/index.jhtml");
	}
}
